package org.altervista.cp.thinjdbc.ds;

import java.sql.Connection;

import javax.sql.DataSource;

public abstract class PoolingDataSource implements DataSource {

	public static final int DEFAULT_INITIAL_POOL_SIZE = 0;
	public static final int DEFAULT_MAX_POOL_SIZE = 10;

	protected abstract ConnectionPool getConnectionPool();

	public PoolingDataSource() {
		_POOL = getConnectionPool();

		setInitialPoolSize(DEFAULT_INITIAL_POOL_SIZE);
		setMaxPoolSize(DEFAULT_MAX_POOL_SIZE);
	}

	public Connection getConnection() {
		return _POOL.poll();
	}

	public Connection getConnection(String username, String password) {
		return _POOL.poll(username, password);
	}

	public int getInitialPoolSize() {
		return _POOL.getInitialSize();
	}

	public int getMaxPoolSize() {
		return _POOL.getMaxSize();
	}

	public void setInitialPoolSize(int initialSize) {
		_POOL.setInitialSize(initialSize);
	}

	public void setMaxPoolSize(int maxSize) {
		_POOL.setMaxSize(maxSize);
	}

	public void shutdown() {
		_POOL.shutdown();
	}

	protected final ConnectionPool _POOL;
}
